package com.tomj.toolbox;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.tomj.toolbox.auto.Auto;
import com.tomj.toolbox.auto.GeneratorFactory;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;


/**
 * @author : tomj
 * @Date: 2019-03-06 17:40
 */
public abstract class AbsCodingMojo extends AbstractMojo {

    @Parameter(name = "model")
    private String model;

    @Parameter(name = "table")
    private String table;

    @Parameter(name = "operationType")
    private String operationType;

    @Parameter(name = "configPath")
    private String configPath;


    protected void selectGenerator(Auto auto){
        auto.excludeXml();
    }

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        Properties properties = new Properties();
        try {
            if(StringUtils.isEmpty(configPath)){
                configPath = System.getProperty("user.dir") + "/mybatis-plus.properties";
            }
            System.out.println(configPath);
            properties.load(Files.newInputStream(Paths.get(configPath)));

            Auto auto = Auto.model(model).table(table);
            auto.setBusType(operationType);
            selectGenerator(auto);
            GeneratorFactory.getInstance(auto,properties).execute();
        } catch (IOException e) {
            System.out.println("读取配置文件出错！");
            e.printStackTrace();
        }

    }
}
